package com.jetlion.demo2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.jetlion.demo2.bean.Student;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //对提交按钮下监听
        submitBtnListen();
    }

    /**
     * 提交按钮的监听
     */
    private void submitBtnListen() {
        Button button = findViewById(R.id.submitBtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //-------------姓名-----------------
                //姓名的编辑框
                EditText editText = findViewById(R.id.realname);

                //获取编辑框中的文字
                String realname = editText.getText().toString();

                //-------------性别-----------------
                //性别的单选框
                RadioGroup radioGroup = findViewById(R.id.sexRadioGroup);

                //获取被选中的RadioButtonId
                int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();

                //根据选中的对性别赋值
                String sex = null;
                switch (checkedRadioButtonId) {
                    case R.id.radio_male:
                        sex = "男";
                        break;
                    case R.id.radio_female:
                        sex = "女";
                        break;
                    default:
                        break;
                }

                //-------------兴趣----------------
                //获取兴趣的复选框选项
                CheckBox runCheckBox = findViewById(R.id.checkbox_run);
                CheckBox swinCheckBox = findViewById(R.id.checkbox_swin);
                CheckBox readingCheckBox = findViewById(R.id.checkbox_reading);

                //兴趣的集合
                List<String> interests = new ArrayList<>();

                if (runCheckBox.isChecked()) {
                    interests.add("跑步");
                }

                if (swinCheckBox.isChecked()) {
                    interests.add("游泳");
                }

                if (readingCheckBox.isChecked()) {
                    interests.add("阅读");
                }

                Student student = new Student(realname, sex, interests);

                Log.i("MainActivity", student.toString());
            }
        });
    }
}